package com.dubbo.service.Impl;

import com.dubbo.mapper.OrderMapper;
import com.dubbo.pojo.Order;
import com.dubbo.pojo.Storage;
import com.dubbo.service.OrderService;
import com.dubbo.service.StorageService;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;


@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Resource
    OrderMapper orderMapper;

    @Reference
    StorageService storageService;

    @GlobalTransactional
    @Override
    public void createOrder(Order order) {
        log.info("开始全局事务。XID:{}", RootContext.getXID());
        Storage storage=new Storage();
        storage.setCommodityCode(order.getCommodityCode());
        storage.setCount(order.getCount());
        //rpc _service
        storageService.decreaseStorage(storage);
        // 本地bean
        orderMapper.createOrder(order);
    }
}
